﻿using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace LeRenard.Libraries.Drawing.GdiPlus.Forms
{
    /// <summary>
    /// List that only keeps a limited amount of items.
    /// </summary>    
    public class LimitedList<T>
    {
        /// <summary>
        /// Holds the wrapped queue
        /// </summary>
        private List<T> _list;

        /// <summary>
        /// Gets/sets the queue's size (limit).
        /// </summary>
        public int Limit { get { return _limit; } }
        private readonly int _limit;

        public IEnumerable<T> Items
        {
            get {
                return new ReadOnlyCollection<T>(_list);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="limit"></param>
        public LimitedList(int limit)
        {            
            _list = new List<T>();
            _limit = limit;
        }

        /// <summary>
        /// Adds an item to the list.
        /// </summary>        
        public void Add(T item)
        {
            if (_list.Count == _limit) {
                var newList = new List<T>();
                for(int i = 1; i < _list.Count; i++){
                    newList.Add(_list[i]);
                }
                _list = newList;
            }

            _list.Add(item);
        }        
    }
}
